﻿using System;
using System.Web;
using System.Web.Security;
using METU_CMT.Business;

namespace METU_CMT
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["User"] != null)
            {
                EntityFactory.Permit();
                lnkButtonLogOut.Visible = true;
                menu.Visible = true;

                User _user = EntityFactory.User;
                _user.SetHome(menu.Items[0]);

                if (Session["MenuItem2"] != null)
                {
                    menu.Items[1].NavigateUrl = Session["MenuItem2"].ToString();
                }
                else
                {
                    menu.Items[1].Enabled = false;
                }
            }
            else if (Request.Url.ToString().Contains("LoginScreen.aspx") == false
                && Request.Url.ToString().Contains("PasswordRecoveryScreen.aspx") == false
                )
            {
                Response.Redirect("~/LoginScreen.aspx");
            }
        }

        protected void OnlnkBtnLogOutClicked(object sender, EventArgs e)
        {
            HttpContext.Current.Session["CourseId"] = null;
            HttpContext.Current.Session["User"] = null;
            HttpContext.Current.Session["CourseInstanceInstructor"] = null;
            HttpContext.Current.Session["CourseInstanceTeachingAssistant"] = null;
            HttpContext.Current.Session["CourseInstanceStudent"] = null;
            HttpContext.Current.Session["Permissions"] = null;
            FormsAuthentication.SignOut();
            Session.Abandon();
            lnkButtonLogOut.Visible = false;
            menu.Visible = false;
            Response.Redirect("~/LoginScreen.aspx");
        }
    }
}
